/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.game.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.util.EventObject;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fi.honkalampisaatio.game.selection.SelectionListener;

public abstract class BasePanel extends JPanel {
	private static final long serialVersionUID = 4015502703829647815L;
	protected SelectionListener listener = null;

	public void setForeground(Color color)
	{
		super.setForeground(color);
		Component[] compList = this.getComponents();
		for(int i=0;i<compList.length;i++)
		{
			compList[i].setForeground(color);
		}
	}

	public void setFont(Font font)
	{
		super.setFont(font);
		Component[] compList = this.getComponents();
		for(int i=0;i<compList.length;i++)
		{
			float fontSize = 12;
			if(compList[i] instanceof JButton)
			{
				fontSize = ((JButton)compList[i]).getFont().getSize2D();
			}
			compList[i].setFont(font.deriveFont(fontSize));
		}
	}
	
	public void addSelectionListener(SelectionListener  listener) {
		this.listener = listener;
	}
	
	public void removeSelectionListener(SelectionListener listener) {
		if (this.listener == listener) {
			this.listener = null;
		}
	}
	
	protected void fireSelectionEvent(EventObject se) {
		if (this.listener != null)
			this.listener.selectionEventOccurred(se);
	}
	
	@SuppressWarnings("rawtypes")
	protected float getFontSizeFromComponents(Component comp, Font font, float fontSize)
	{
		FontMetrics fm;
		float oldFontSize = fontSize;
		int textMaxWidthWidth = (int) (comp.getWidth()*0.8);
		int textMaxWidthHeight = (int) (comp.getHeight()*0.5);
		if(comp instanceof Container)
		{
			Component[] compList = ((Container)comp).getComponents();
			for(int i=0;i<compList.length;i++)
			{
				fontSize = getFontSizeFromComponents(compList[i], font, fontSize);
			}
		}
		if(comp instanceof JLabel)
		{
			JLabel c = (JLabel)comp;
			fm = c.getFontMetrics(font.deriveFont(fontSize));
			if(fm.getHeight()>textMaxWidthHeight && fm.getHeight()>0 && !c.getText().isEmpty())
			{
				fontSize = fontSize * textMaxWidthHeight / fm.getHeight();
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
			if(fm.stringWidth(c.getText())>textMaxWidthWidth && fm.stringWidth(c.getText())>0)
			{
				fontSize = fontSize * textMaxWidthWidth / fm.stringWidth(c.getText());
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
		}
		else if(comp instanceof JTextField)
		{
			JTextField c = (JTextField)comp;
			fm = c.getFontMetrics(font.deriveFont(fontSize));
			if(fm.getHeight()>textMaxWidthHeight && fm.getHeight()>0 && !c.getText().isEmpty())
			{
				fontSize = fontSize * textMaxWidthHeight / fm.getHeight();
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
			if(fm.getMaxAdvance()*c.getColumns()>textMaxWidthWidth && fm.getMaxAdvance()*c.getColumns()>0)
			{
				fontSize = fontSize * textMaxWidthWidth / fm.getMaxAdvance()*c.getColumns();
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
		}
		else if(comp instanceof ScalableIconButton)
		{
			ScalableIconButton c = (ScalableIconButton)comp;
			fm = c.getFontMetrics(font.deriveFont(fontSize));
			if(fm.getHeight()>c.getTextMaxDimension().height && fm.getHeight()>0 && !c.getText().isEmpty())
			{
				fontSize = fontSize * c.getTextMaxDimension().height / fm.getHeight();
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
			if(fm.stringWidth(c.getText())>c.getTextMaxDimension().width && fm.stringWidth(c.getText())>0)
			{
				fontSize = fontSize * c.getTextMaxDimension().width / fm.stringWidth(c.getText());
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
		}
		else if(comp instanceof AbstractButton)
		{
			AbstractButton c = (AbstractButton)comp;
			fm = c.getFontMetrics(font.deriveFont(fontSize));
			if(fm.getHeight()>textMaxWidthHeight && fm.getHeight()>0 && !c.getText().isEmpty())
			{
				fontSize = fontSize * textMaxWidthHeight / fm.getHeight();
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
			if(fm.stringWidth(c.getText())>textMaxWidthWidth && fm.stringWidth(c.getText())>0)
			{
				fontSize = fontSize * textMaxWidthWidth / fm.stringWidth(c.getText());
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
		}
		else if(comp instanceof JComboBox)
		{
			JComboBox c = (JComboBox)comp;
			fm = c.getFontMetrics(font.deriveFont(fontSize));
			if(fm.getHeight()>textMaxWidthHeight/2 && fm.getHeight()>0)
			{
				fontSize = fontSize * textMaxWidthHeight/2 / fm.getHeight();
				fm = c.getFontMetrics(font.deriveFont(fontSize));
			}
			for(int cbi=0; cbi<c.getItemCount(); cbi++)
			{
				String itemText = c.getItemAt(cbi).toString();
				if(fm.stringWidth(itemText)>textMaxWidthWidth && fm.stringWidth(itemText)>0)
				{
					fontSize = fontSize * textMaxWidthWidth / fm.stringWidth(itemText);
					fm = c.getFontMetrics(font.deriveFont(fontSize));
				}
			}
		}
		if(fontSize==0)
		{
			System.out.println("fontSize 0 " + comp.getClass().getSimpleName());
			fontSize = oldFontSize;
		}

		
		return fontSize;
	}

	protected void setFontForComponents(Component comp, Font font)
	{
		comp.setFont(font);
		if(comp instanceof Container)
		{
			Component[] compList = ((Container)comp).getComponents();
			for(int i=0;i<compList.length;i++)
			{
				setFontForComponents(compList[i], font);
			}
		}
	}
	
	protected void resizeTexts()
	{
		float fontSize = 144;
		fontSize = getFontSizeFromComponents(this, this.getFont().deriveFont(fontSize), fontSize);
		setFontForComponents(this, this.getFont().deriveFont(fontSize));
	}
}
